package net.sunofbeaches.dao;

import net.sunofbeaches.pojo.SobUser;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface UserDao extends JpaRepository<SobUser, String>, JpaSpecificationExecutor<SobUser> {
    /**
     * 查找UserName
     *
     * @param username
     * @return
     */
    SobUser findOneByUserName(String username);

    /**
     * 查找email
     *
     * @param email
     * @return
     */
    SobUser findOneByEmail(String email);

    /**
     * 查询两个 或语句 where XX = '' and XX = ''
     *
     * @param email
     * @param userName
     * @return
     */
    SobUser findOneByEmailOrUserName(String email, String userName);

    SobUser findOneById(String id);

    @Modifying
    @Query(nativeQuery = true, value = "UPDATE `tb_user` SET `state` = '0' WHERE `id`= ?")
    int deleteUserByState(String userId);

    @Query(value = "select new SobUser (u.id,u.userName,u.roles,u.avatar,u.email,u.sign,u.state,u.regIp,u.loginIp,u.createTime,u.updateTime) from SobUser as u ")
    Page<SobUser> listAllUserNoPassword(Pageable pageable);

    @Modifying
    @Query(nativeQuery = true, value = "UPDATE `tb_user` SET `password` = ? WHERE `email`=?")
    int updatePasswordByEmail(String encode,String email);

    @Modifying
    @Query(nativeQuery = true, value = "UPDATE `tb_user` SET `email` = ? WHERE `id`=?")
    int updateEmailById(String encode,String email);
}





